// pages/coupons/coupons.js
const api = require("../../server/Api.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabTitles: [
      { name: "未使用", type: 0 }, { name: "已使用", type: 1 },
      { name: "已过期", type: 3 }],
    emptyType: 2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //头部导航栏选择事件
  tabSelect: function (e) {
    let tabBean = e.currentTarget.dataset.tab;
    this.setData({
      curTap: tabBean.type,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
    this.refreshData();
  },
  //刷新列表
  refreshData: function () {
    // tt.showNavigationBarLoading();
    var that = this;
    this.setData({
      hasMore: true,
    })
    this.queryOrderList(1, this.data.pageSize);
  },
  //分页查询订单
  queryOrderList: function (page, pageSize) {
    let that = this;
    let userBean = app.globalData.userBean;
    let orderStatus = this.data.curTap;
    let prams = {
      pageSize: that.data.pageSize,
      pageNumber: page,
      userId: userBean.consumerId,
      status: orderStatus
    };

    api.queryOrderList(prams).then(res => {
      //请求成功
      that.querySuccess(res, page)
      tt.stopPullDownRefresh();
      that.setData({
        emptyType: 2
      })
    }).catch(msg => {
      tt.stopPullDownRefresh();
      that.setData({
        emptyType: 101
      })
      var message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
    })
  },
  //查询成功
  querySuccess: function (res, curPage) {
    let that = this;
    //数据
    let records = res.records;
    let maxPage = parseInt(res.pages);
    var datas = curPage == 1 ? [] : this.data.items;
    if (records) {
      datas = datas.concat(records);
    }
    that.setData({
      hasMore: maxPage > curPage,
      items: datas,
      curPage: curPage,
    })
  },
  
})